package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.FenLeiBiao;
import com.ysd.util.DBManager;

public class FenLeiBiaoDaoimpl {
	/**
	 * 获得产品分类表的数据
	 * @return
	 */
	public ArrayList<FenLeiBiao> getFenLeiBiao(){
		ArrayList<FenLeiBiao> list=new ArrayList<FenLeiBiao>();
		Connection conn=DBManager.getConnection();
		Statement st=null;
		ResultSet rs=null;
		try {
			 st=conn.createStatement();
			String sql="SELECT * FROM fenleibiao";
			 rs=st.executeQuery(sql);
			while(rs.next()) {
				FenLeiBiao flb=new FenLeiBiao(rs.getInt("chanPinId"), rs.getString("leiMing"));
				list.add(flb);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	
	
	public ArrayList<FenLeiBiao> getFenLeiBiaoByPage(int page,int size){
		ArrayList<FenLeiBiao> list=new ArrayList<FenLeiBiao>();
		Connection conn=DBManager.getConnection();
		Statement st=null;
		ResultSet rs=null;
		try {
			 st=conn.createStatement();
			String sql="select * from fenleibiao limit "+(page-1)*size+", "+size;
			 rs=st.executeQuery(sql);
			while(rs.next()) {
				FenLeiBiao flb=new FenLeiBiao(rs.getInt("chanPinId"), rs.getString("leiMing"));
				list.add(flb);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	
	
	
	/**
	 * 根据id删除对应的数据
	 * @param id
	 * @return
	 */
	public int delChanPinFenLei(int id) {
		return DBManager.executeUpdate("delete from fenleibiao where chanPinId="+id);
	}
	
	
	
	public int addChenPinFenLei(FenLeiBiao f) {
		return DBManager.executeUpdate("INSERT into  fenleibiao VALUES (null,'"+f.getLeiMing()+"')");
	}
	
	/**
	 * 通过分类名称查询
	 * @param name
	 * @return
	 */
	public ArrayList<FenLeiBiao> getLeiMingByName(String name){
		ArrayList<FenLeiBiao> list=new ArrayList<FenLeiBiao>();
		Connection conn=DBManager.getConnection();
		Statement st=null;
		ResultSet rs=null;
		try {
			 st =conn.createStatement();
			 rs=st.executeQuery("select * from fenleibiao where leiming='"+name+"'");
			while(rs.next()) {
				int chanPinId=rs.getInt("chanPinId");
				String leiMing=rs.getString("leiMing");
				FenLeiBiao f=new FenLeiBiao(chanPinId, leiMing);
				list.add(f);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
		
		
		
	}
	
	
	
	/**
	 * 根据id查询信息
	 * @param id
	 * @return
	 */
	public FenLeiBiao getFenLeiById(int id) {
		Connection conn=DBManager.getConnection();
		Statement st=null;
		ResultSet rs=null;
		FenLeiBiao flb=null;
		try {
			st=conn.createStatement();
			rs=st.executeQuery("SELECT * FROM fenleibiao WHERE chanpinid="+id);
			while(rs.next()) {
				 flb=new FenLeiBiao(rs.getInt("chanPinId"), rs.getString("leiMing"));
				
			}
			return flb;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	
	}
	
	public int editChanPinFenLeiById(FenLeiBiao f) {
		String sql="UPDATE fenleibiao set leiMing='"+f.getLeiMing()+"' where chanPinId="+f.getChanPinId();
	
		return DBManager.executeUpdate(sql);
	}
	
	
	public List<FenLeiBiao> getFenLeiSize() {
		Connection conn=DBManager.getConnection();
		Statement st=null;
		ResultSet rs=null;
		FenLeiBiao flb=null;
		List<FenLeiBiao> list=new ArrayList<FenLeiBiao>();
		try {
			st=conn.createStatement();
			rs=st.executeQuery("select chanpinId from fenleibiao");
			while(rs.next()) {
				 flb=new FenLeiBiao(rs.getInt("chanPinId"),"null");
				list.add(flb);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	
	}
	
}
